package org.csu.jpetstore_users_backend_pj.service.impl;

import org.csu.jpetstore_users_backend_pj.entity.Account;
import org.csu.jpetstore_users_backend_pj.entity.BannerData;
import org.csu.jpetstore_users_backend_pj.entity.Profile;
import org.csu.jpetstore_users_backend_pj.entity.SignOn;
import org.csu.jpetstore_users_backend_pj.mapper.AccountMapper;
import org.csu.jpetstore_users_backend_pj.mapper.BannerDataMapper;
import org.csu.jpetstore_users_backend_pj.mapper.ProfileMapper;
import org.csu.jpetstore_users_backend_pj.mapper.SignOnMapper;
import org.csu.jpetstore_users_backend_pj.service.AccountService;
import org.csu.jpetstore_users_backend_pj.util.EncryptUtil;
import org.csu.jpetstore_users_backend_pj.vo.AccountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private SignOnMapper signOnMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;
    @Autowired
    private EncryptUtil encryptUtil;

    @Override
    public void updateAccount(AccountVO accountVO) {
        accountMapper.updateById(accountVO.getAccount());
        profileMapper.updateById(accountVO.getProfile());
    }

    @Override
    public String getBannerName(String favouriteCategoryId) {
        BannerData bannerData = bannerDataMapper.selectById(favouriteCategoryId);
        return bannerData.getBannerName();
    }

    @Override
    public AccountVO getAccountVOByUsername(String username) {
        Account account = accountMapper.getAccountByUsername(username);
        Profile profile = profileMapper.selectById(username);
        String bannerName = bannerDataMapper.selectById(profile.getFavouriteCategoryId()).getBannerName();
        String pwdHash = accountMapper.getPwdHashByUsername(username);
        AccountVO accountVO = new AccountVO();
//        BeanUtils.copyProperties(account, accountVO);
//        BeanUtils.copyProperties(profile, accountVO);
//        BeanUtils.copyProperties(signOn, accountVO);
        accountVO.setAccountVO(account,profile,bannerName);
        accountVO.setPwdHash(pwdHash);
        return accountVO;
    }

    @Override
    public void changePassword(String username, String password) {
        accountMapper.updatePassword(username, encryptUtil.encryptPwd(password));
    }

//    @Override
//    public String getPassword(String username) {
//        SignOn signOn=signOnMapper.selectById(username);
//        return signOn.getPwdHash();
//    }

}
